package com.fenrirtec.aepp.common.api;

import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.alibaba.fastjson.JSONObject;
import com.fenrirtec.aepp.common.dto.UserBidRecordsDto;
import com.fenrirtec.aepp.common.service.TenderBidService;
import com.fenrirtec.aepp.common.service.impl.TenderBidServiceImpl;
import com.fenrirtec.aepp.common.support.JSONActionSupport;
import com.fenrirtec.core.definition.BaseDefinition.API;
import com.fenrirtec.core.exception.DatabaseException;
import com.fenrirtec.core.exception.UnexpectedException;
import com.fenrirtec.core.utils.JsonUtils;

public class TenderBidSearchAPI extends JSONActionSupport{

	private static final long serialVersionUID = 1L;

	private static final Logger logger = LoggerFactory.getLogger(TenderBidSearchAPI.class);

	public TenderBidSearchAPI() {
	}
	
	private String param;
    private int _page;	
	private int _rows;

	public String getParam() {
		return this.param;
	}

	public void setParam(String param) {
		this.param = param;
	}

	public int get_page() {
		return _page;
	}

	public void set_page(int _page) {
		this._page = _page;
	}

	public int get_rows() {
		return _rows;
	}

	public void set_rows(int _rows) {
		this._rows = _rows;
	}

	@Override
	protected List<ErrorInfo> doValidate() {
		return null;
	}

	@Override
	protected JSONObject doExecute() {
		TenderBidService service = new TenderBidServiceImpl();
		List<UserBidRecordsDto> userBidRecordsDtoList = null;
		Integer tenderEnrollCount = 0;
		
		try {
			JSONObject jsonObj = JSONObject.parseObject(this.param);
			
			String memberLoginName = jsonObj.getString("member_login_name");
			userBidRecordsDtoList = service.searchByCnd(memberLoginName);
			
		} catch (DatabaseException e) {
			if (logger.isErrorEnabled()) {
				logger.error("database error occurred.", e);
			}
			throw new JSONActionStatusException(API.Common.Status.DB_ERROR, e);
		} catch (UnexpectedException e) {
			if (logger.isErrorEnabled()) {
				logger.error("unexpected error occurred.", e);
			}
			throw new JSONActionStatusException(API.Common.Status.DB_ERROR, e);
		}
		tenderEnrollCount = userBidRecordsDtoList.size();
		JSONObject root = new JSONObject();
		root.put("total", tenderEnrollCount);
		if (userBidRecordsDtoList != null && userBidRecordsDtoList.size() > 0) {
			root.put("tenderEnrolls", JsonUtils.fromBean(userBidRecordsDtoList));
		}
		return root;
	}

}
